package com.zy.mc_module.ui.shortvideo.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zy.mc_module.R;
import com.zy.mc_module.mvp.bean.ShortVideoBean;
import com.zy.mc_module.ui.shortvideo.VideoPresenter;
import com.zy.mc_module.utils.GlideUtil;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.utils.UserUtils;
import com.zy.mc_module.view.CheckedImageView;

import java.util.ArrayList;

/**
 * @author 几圈年轮
 * @date 2019/12/18.
 * description：适配器内容
 */
public class LittleVideoAdapter extends BaseQuickAdapter<ShortVideoBean, BaseViewHolder> {

    private Context context;
    private VideoPresenter presenter;

    public LittleVideoAdapter(Context context, VideoPresenter presenter) {
        super(R.layout.littlevideo_recyclerview_item, new ArrayList<>());
        this.context = context;
        this.presenter = presenter;
    }

    @Override
    protected void convert(BaseViewHolder helper, ShortVideoBean item) {
        RequestOptions options = new RequestOptions()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .frame(1);
        Glide.with(mContext).setDefaultRequestOptions(options).load(item.getVideoUrl()).into((ImageView) helper.getView(R.id.iv_thumb_item));
        GlideUtil.displayImage(helper.getView(R.id.input_iv_logo), item.getCreateHead(), R.mipmap.ic_launcher);
        helper.addOnClickListener(R.id.input_tv_seemsg);
        helper.addOnClickListener(R.id.input_rl_share);
        helper.addOnClickListener(R.id.video_detail_recommend_shop);
        helper.setText(R.id.video_detail_recommend_shop, item.getGoodsName());
        helper.setGone(R.id.video_detail_recommend_shop, !StringsUtils.isEmpty(item.getGoodsName()));
        helper.setText(R.id.input_tv_like, item.getSupportCount());
        helper.setText(R.id.input_tv_seemsg, item.getDiscussCount());
        helper.setText(R.id.input_tv_share, item.getShareCount());
        helper.setText(R.id.video_detail_name, String.format("@%s",item.getCreateBy()));
        TextView tvCon = helper.getView(R.id.video_detail_con);
        tvCon.setText(Html.fromHtml(item.getContent()));
        CheckedImageView iv_like = helper.getView(R.id.input_iv_like);
        TextView tv_like = helper.getView(R.id.input_tv_like);
        if (item.getIsGreat() == 0) {
            iv_like.setImageRes(R.mipmap.icon_short_video_unlike, false);
        } else if (item.getIsGreat() == 1) {
            iv_like.setImageRes(R.mipmap.icon_short_video_like, true);
        }
        iv_like.setOnClickListener(v -> {
            if (UserUtils.isLogin()) {
                //presenter.isLike(context, String.valueOf(item.getArticleId()));
                if (iv_like.getChecked()) {
                    //未点赞
                    iv_like.setImageRes(R.mipmap.icon_short_video_unlike, false);
                } else {
                    //点赞
                    iv_like.setImageRes(R.mipmap.icon_short_video_like, true);
                }
                refreshNum(tv_like, iv_like.getChecked());
            } else {
                //context.startActivity(new Intent(context, LoginActivity.class));
            }
        });
    }


    /***
     * 刷新 点赞 收藏 数
     * @param isChecked 为true +1, 为false -1
     */
    public void refreshNum(TextView text, boolean isChecked) {
        try {
            int num = Integer.parseInt(text.getText().toString());
            if (isChecked) {
                //加
                text.setText((num + 1) + "");
            } else {
                //减
                text.setText((num - 1) + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
